import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/app-component-base';
import { Component, OnInit, ViewChild, EventEmitter, Output, Injector } from '@angular/core';
import { ProductServiceProxy, AddToInventoryInputDto } from '@shared/service-proxies/service-proxies';

@Component({
  selector: 'app-product-inventor',
  templateUrl: './product-inventor.component.html',
  styleUrls: ['./product-inventor.component.css']
})
export class ProductInventorComponent extends AppComponentBase {
  @ViewChild('addToInventorModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active: boolean = false;
  saving: boolean = false;
  inventoryDto: AddToInventoryInputDto = null;
  constructor(
    injector: Injector,
    private _productService: ProductServiceProxy
  ) {
    super(injector);
  }

  show(id?: number): void {
    this.active = true;
    this.inventoryDto = new AddToInventoryInputDto();
    this.inventoryDto.id = id;
    this.modal.show();
  }
  close(): void {
    this.active = false;
    this.modal.hide();
  }
  save(): void {
    this.saving = true;
    this._productService.addToInventoryAsync(this.inventoryDto)
      .finally(() => { this.saving = false; })
      .subscribe(() => {
        this.notify.info('保存成功!');
        this.close();
        this.modalSave.emit(null);
      });
  }

}
